//
//  SwiftViewController.swift
//  Charles
//
//  Created by Leo on 2019/8/13.
//  Copyright © 2019 charles. All rights reserved.
//

import UIKit
import SnapKit
import Moya

let provider = MoyaProvider<MultiTarget>(plugins: [NetworkLoggerPlugin(verbose: true)])

class SwiftViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    var dataArray : NSMutableArray = []
    var tableView : UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .white
        // Do any additional setup after loading the view.
        self.makeView()
    }
    
    func makeView() -> Void {
        self.tableView = UITableView()
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.view.addSubview(self.tableView)
      
        // 设置布局
        self.tableView.snp.makeConstraints { (make)->Void in
            make.left.right.equalToSuperview()
            make.top.bottom.equalToSuperview()
        }
    }
    
    fileprivate func showAlert(_ title: String, message: String) {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
        let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
        alertController.addAction(okAction)
        present(alertController, animated: true, completion: nil)
    }
    
    func setDataArray(array:NSMutableArray) -> Void {
        self.dataArray = NSMutableArray (array: array)
    }
    
    func loadDataRequest() -> Void {
        provider.request(MultiTarget(GitHub.zen)) { result in
            var message = "Couldn't access API"
            if case let .success(response) = result {
                let jsonStirng = try? response.mapString()
                message = jsonStirng ?? message
            }
            
            self.showAlert("Zen", message: message)
        }
    }
    
    // MARK: - UITableView
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10;
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 70;
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 1 {
            loadDataRequest();
        }
        print(indexPath.row)
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let identifier = "SwiftCell"
        var cell: UITableViewCell! = tableView.dequeueReusableCell(withIdentifier: identifier)
        if cell == nil {
            cell =  UITableViewCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: identifier)
        }
        cell.textLabel?.text = String(format: "第 %d 行", indexPath.row)
        return cell
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
